package com.wanma.recuit.webcontroller;

import com.wanma.recuit.pojo.*;
import com.wanma.recuit.service.DictionaryService;
import com.wanma.recuit.service.ProvinceCityRegionService;
import com.wanma.recuit.service.ResumeService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/service/user")
public class ResumeController {

    @Resource
    private ResumeService resumeService;
    @Resource
    private DictionaryService dictionaryService;
    @Resource
    private ProvinceCityRegionService provinceCityRegionService;

    //根据用户id进入我的简历编辑页面
    @RequestMapping("/myResume")
    public ModelAndView myResume(Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        Resume resume = resumeService.findById(id);
        modelAndView.addObject("myResume",resume);
        Dictionary userSate = dictionaryService.findUserSate();
        Dictionary salary = dictionaryService.findSalary();
        Dictionary kind = dictionaryService.findKind();
        Dictionary education = dictionaryService.findEducation();
        Dictionary fulltime = dictionaryService.findFulltime();
        Dictionary experience = dictionaryService.findExperience();
        modelAndView.addObject("userSate",userSate);
        modelAndView.addObject("salary",salary);
        modelAndView.addObject("kind",kind);
        modelAndView.addObject("education",education);
        modelAndView.addObject("fulltime",fulltime);
        modelAndView.addObject("experience",experience);
        List<ProvinceCityRegion> provinceCityRegionList = provinceCityRegionService.findAll(0);
        modelAndView.addObject("provinceCityRegion",provinceCityRegionList);
        return modelAndView;
    }

    //根据用户id进入简历预览界面
    @RequestMapping("/resumePreview")
    @ModelAttribute("resume")
    public Resume resumePreview(Integer id)
    {
        return resumeService.findById(id);
    }

    //更新用户头像
    @RequestMapping("/updatePic")
    @ResponseBody
    public String updatePic(MultipartFile file,HttpServletRequest request)
    {
        String originalFileName = file.getOriginalFilename();
        String ext = FilenameUtils.getExtension(originalFileName);
        if (!",jpg,jpeg,bmp,gif,png,".contains("," + ext + ",")) {
            return "extError";
        }
        String fileName = UUID.randomUUID() + "." + ext;
        String realPath = request.getServletContext().getRealPath("/static/images");
        File newPic = new File(realPath,fileName);
        try {
            file.transferTo(newPic);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName;
    }

    //更新用户基本资料
    @RequestMapping("/updateUser")
    @ResponseBody
    public void updateUser(Integer id,Integer recuitId,String name,String sex,Integer education,Integer experience,String phone,String email,Integer userSate,String pic)
    {
        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setSex(sex);
        Dictionary edu = new Dictionary();
        edu.setId(education);
        user.setEducation(edu);
        Dictionary exp = new Dictionary();
        exp.setId(experience);
        user.setExperience(exp);
        user.setPhone(phone);
        user.setEmail(email);
        Dictionary sate = new Dictionary();
        sate.setId(userSate);
        user.setUserSate(sate);
        user.setPic(pic);
        resumeService.updateUser(user);
        resumeService.updateUpdateTime(recuitId);
    }

    //添加用户期望
    @RequestMapping("/insertExpectation")
    @ResponseBody
    public void insertExpectation(Integer recuitId,Integer city,Integer kind,String postion,Integer salary)
    {
        Expectation expectation = new Expectation();
        expectation.setRecuitId(recuitId);
        ProvinceCityRegion provinceCityRegion = new ProvinceCityRegion();
        provinceCityRegion.setId(city);
        expectation.setCity(provinceCityRegion);
        Dictionary dicKind = new Dictionary();
        dicKind.setId(kind);
        expectation.setKind(dicKind);
        expectation.setPostion(postion);
        Dictionary dicSalary = new Dictionary();
        dicSalary.setId(salary);
        expectation.setSalary(dicSalary);
        resumeService.insertExpectation(expectation);
        resumeService.updateUpdateTime(recuitId);
    }

    //添加工作经历
    @RequestMapping("/insertWorkExperience")
    @ResponseBody
    public void insertWorkExperience(Integer recuitId, String companyName, String positionName, String jobDescription, String timeStart,String timeEnd)
    {
        Date startTime =null;
        Date endTime = null;
        //获得SimpleDateFormat类，我们转换为yyyy-MM-dd的时间格式
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
            //使用SimpleDateFormat的parse()方法生成Date
        try {
            startTime = (Date) sf.parse(timeStart);
            endTime = (Date) sf.parse(timeEnd);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        WorkExperience workExperience = new WorkExperience();
        workExperience.setCompanyName(companyName);
        workExperience.setRecuitId(recuitId);
        workExperience.setPositionName(positionName);
        workExperience.setJobDescription(jobDescription);
        workExperience.setEntryTime(startTime);
        workExperience.setResignTime(endTime);
        resumeService.insertWorkExperience(workExperience);
        resumeService.updateUpdateTime(recuitId);
    }

    //添加教育经历
    @RequestMapping("/insertEduExperience")
    @ResponseBody
    public void insertEduExperience(Integer recuitId,String schoolName,Integer education,String professionalName,Integer fulltime,String yearStart,String yearEnd)
    {
        Date startTime =null;
        Date endTime = null;
        //获得SimpleDateFormat类，我们转换为yyyy-MM-dd的时间格式
        SimpleDateFormat sf = new SimpleDateFormat("yyyy");
        //使用SimpleDateFormat的parse()方法生成Date
        try {
            startTime = (Date) sf.parse(yearStart);
            endTime = (Date) sf.parse(yearEnd);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        EduExperience eduExperience = new EduExperience();
        eduExperience.setRecuitId(recuitId);
        eduExperience.setSchoolName(schoolName);
        Dictionary dicEdu = new Dictionary();
        dicEdu.setId(education);
        eduExperience.setEducation(dicEdu);
        eduExperience.setProfession(professionalName);
        Dictionary dicfulltime = new Dictionary();
        dicfulltime.setId(fulltime);
        eduExperience.setFulltime(dicfulltime);
        eduExperience.setEntryTime(startTime);
        eduExperience.setGraductionTime(endTime);
        resumeService.insertEduExperience(eduExperience);
        resumeService.updateUpdateTime(recuitId);
    }

    //更新自我描述
    @RequestMapping("/updateIntroduction")
    @ResponseBody
    public void updateIntroduction(String selfDescription,Integer userId)
    {
        resumeService.updateIntroduction(selfDescription,userId);
    }
}
